package com.blockmeta.bbs.businesslibrary.widget.chartview.Layer;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;

import com.blockmeta.bbs.baselibrary.base.app.BaseApp;
import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.businesslibrary.R;
import com.blockmeta.bbs.businesslibrary.websocket.Symbol;
import com.blockmeta.bbs.businesslibrary.widget.chartview.Axis.Coordinate;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CandleData;
import com.blockmeta.bbs.businesslibrary.widget.chartview.CoinChart;
import com.blockmeta.bbs.businesslibrary.widget.tictactec.ta.lib.Core;
import com.blockmeta.bbs.businesslibrary.widget.tictactec.ta.lib.MInteger;
import com.blockmeta.bbs.businesslibrary.widget.tictactec.ta.lib.RetCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * kbar和ma的合并
 */
class KBarMaMoveLayer extends KBarMoveLayer
{
	private int beginIdx1, beginIdx2, beginIdx3, beginIdx4, beginIdx5, beginIdx6, beginIdx7;//用于存储第一个指标第一个值的位置
	private int mPeriod1 = 5;
	private int mPeriod2 = 10;
	private int mPeriod3 = 20;
	private int mPeriod4 = 0;
	private int mPeriod5 = 0;
	private int mPeriod6 = 0;
	private int mPeriod7 = 0;
	private int validPeriodCount;
	private float mTxtXLocation;
	private int mNeedDrawIndex;
	private boolean mIsPortrait;
	float txtSize;

	private String m_ma1 = "", m_ma2 = "", m_ma3 = "", m_ma4 = "", m_ma5 = "", m_ma6 = "", m_ma7 = "";
	private String m_lastMa1 = "", m_lastMa2 = "", m_lastMa3 = "", m_lastMa4 = "", m_lastMa5 = "", m_lastMa6 = "", m_lastMa7 = "";
	private int m_color1 = 0xFF3898FF, m_color2 = 0xFFFF72A5, m_color3 = 0xFFFFA200, m_color4 = 0xFF5634FF, m_color5 = 0xFF2CB9E9, m_color6 = 0xFF3CD693, m_color7 = 0xFFFF424A;

	public KBarMaMoveLayer()
	{
		super();
	}


	@Override
	public boolean onCalculateData()
	{
		boolean m_ma_on = true;//FDTStorage.getInstance().getInteger(SaveKey.CHART_INDICATOR_MA + "on", SaveKey.CHART_INDICATOR_PARAM, 1) == 1;
		if (!m_ma_on)
		{
			tMAList = null;
			m_callback.setAdditionData(tMAList);//用于合并均线数据和k线数据的上下限
			return true;
		}

		ArrayList<String> params = new ArrayList<>(Arrays.asList("5", "10", "20", "", "", "", ""));//FDTStorage.getInstance().getArray(SaveKey.CHART_INDICATOR_MA + "param", SaveKey.CHART_INDICATOR_PARAM);
		if (params != null)
		{
			mPeriod1 = FormatUtil.parseInt(params.get(0));
			mPeriod2 = FormatUtil.parseInt(params.get(1));
			mPeriod3 = FormatUtil.parseInt(params.get(2));
			mPeriod4 = FormatUtil.parseInt(params.get(3));
			mPeriod5 = FormatUtil.parseInt(params.get(4));
			mPeriod6 = FormatUtil.parseInt(params.get(5));
			mPeriod7 = FormatUtil.parseInt(params.get(6));
			validPeriodCount = getValidCount(mPeriod1, mPeriod2, mPeriod3, mPeriod4, mPeriod5, mPeriod6, mPeriod7);
		}

		beginIdx1 = Integer.MAX_VALUE;
		beginIdx2 = Integer.MAX_VALUE;
		beginIdx3 = Integer.MAX_VALUE;
		beginIdx4 = Integer.MAX_VALUE;
		beginIdx5 = Integer.MAX_VALUE;
		beginIdx6 = Integer.MAX_VALUE;
		beginIdx7 = Integer.MAX_VALUE;

		List<CandleData> tDataList = m_callback.getCandleDataList();
		int tListSize = tDataList.size();
		tMAList = new ArrayList<CandleData>();
		double[] closes = new double[tListSize];

		for (int ii = 0; ii < tListSize; ii++)
		{
			closes[ii] = tDataList.get(ii).getDbClose();
		}
		//准备计算数组
		Core taLib = CoinChart.getTaLib();
		double[] ma1 = new double[tListSize];
		double[] ma2 = new double[tListSize];
		double[] ma3 = new double[tListSize];
		double[] ma4 = new double[tListSize];
		double[] ma5 = new double[tListSize];
		double[] ma6 = new double[tListSize];
		double[] ma7 = new double[tListSize];
		if (mPeriod1 != 0 && mPeriod1 <= tListSize)
		{
			beginIdx1 = doMaCalculate(closes, taLib, mPeriod1, ma1);
		}
		if (mPeriod2 != 0 && mPeriod2 <= tListSize)
		{
			beginIdx2 = doMaCalculate(closes, taLib, mPeriod2, ma2);
		}
		if (mPeriod3 != 0 && mPeriod3 <= tListSize)
		{
			beginIdx3 = doMaCalculate(closes, taLib, mPeriod3, ma3);
		}
		if (mPeriod4 != 0 && mPeriod4 <= tListSize)
		{
			beginIdx4 = doMaCalculate(closes, taLib, mPeriod4, ma4);
		}
		if (mPeriod5 != 0 && mPeriod5 <= tListSize)
		{
			beginIdx5 = doMaCalculate(closes, taLib, mPeriod5, ma5);
		}
		if (mPeriod6 != 0 && mPeriod6 <= tListSize)
		{
			beginIdx6 = doMaCalculate(closes, taLib, mPeriod6, ma6);
		}
		if (mPeriod7 != 0 && mPeriod7 <= tListSize)
		{
			beginIdx7 = doMaCalculate(closes, taLib, mPeriod7, ma7);
		}


		//得到结果数组,结果为有值的部分开始如0.006747350439404443 0.010591595301185919 0.0027601354459569905 -0.03422737187350222 -0.09373321549064784 -0.12136830634374718 -0.12757081986248053 -0.1250403123559245 -0.11954476938285197 -0.04594994628476812 0.009701854832457002 0.06010755762170561 0.09747712313834 0.13895129756983238 0.171272192598785 0.18158545677729165 0.15978420503275714 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0
		//outBegIdx为是第几个开始的，outNbElement为有效的数字个数 ，如73个元素，那么outBegIdx为33，outNbElement为40 26-1+9-1=33
		CandleData candle = null;
		if (tListSize > 0)
		{
			double tMax = Double.MIN_VALUE, tMin = Double.MAX_VALUE;
			int i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0, i6 = 0, i7 = 0;
			for (int i = 0; i < tListSize; i++)//修改数据源
			{
				candle = new CandleData();

				double max = Double.MIN_VALUE;
				double min = Double.MAX_VALUE;
				if (i >= beginIdx1 && mPeriod1 != 0)
				{
					double td = ma1[i1];
					candle.setDbOpen(td);
					if (td > max)
					{
						max = td;
					}
					if (td < min)
					{
						min = td;
					}
					i1++;
				}
				else
				{
					candle.setDbOpen(0.0);
				}
				if (i >= beginIdx2 && mPeriod2 != 0)
				{
					double td = ma2[i2];
					candle.setDbClose(td);
					if (td > max)
					{
						max = td;
					}
					if (td < min)
					{
						min = td;
					}
					i2++;
				}
				else
				{
					candle.setDbClose(0.0);
				}
				if (i >= beginIdx3 && mPeriod3 != 0)
				{
					double td = ma3[i3];
					candle.setDbHigh(td);
					if (td > max)
					{
						max = td;
					}
					if (td < min)
					{
						min = td;
					}
					i3++;
				}
				else
				{
					candle.setDbHigh(0.0);
				}
				if (i >= beginIdx4 && mPeriod4 != 0)
				{
					double td = ma4[i4];
					candle.setOpenBackward(td);
					if (td > max)
					{
						max = td;
					}
					if (td < min)
					{
						min = td;
					}
					i4++;
				}
				else
				{
					candle.setOpenBackward(0.0);
				}
				if (i >= beginIdx5 && mPeriod5 != 0)
				{
					double td = ma5[i5];
					candle.setCloseBackward(td);
					if (td > max)
					{
						max = td;
					}
					if (td < min)
					{
						min = td;
					}
					i5++;
				}
				else
				{
					candle.setCloseBackward(0.0);
				}
				if (i >= beginIdx6 && mPeriod6 != 0)
				{
					double td = ma6[i6];
					candle.setHighBackward(td);
					if (td > max)
					{
						max = td;
					}
					if (td < min)
					{
						min = td;
					}
					i6++;
				}
				else
				{
					candle.setHighBackward(0.0);
				}
				if (i >= beginIdx7 && mPeriod7 != 0)
				{
					double td = ma7[i7];
					candle.setLowBackward(td);
					if (td > max)
					{
						max = td;
					}
					if (td < min)
					{
						min = td;
					}
					i7++;
				}
				else
				{
					candle.setLowBackward(0.0);
				}

				//再把最大和最小算出来放到两个字段里面
				candle.setHighForward(max);
				candle.setLowForward(min);

				tMAList.add(candle);
			}


			m_callback.setAdditionData(tMAList);//用于合并均线数据和k线数据的上下限
		}
		return true;
	}

	private int getValidCount(int... param)
	{
		int validCount = 0;
		for (int i = 0; i < param.length; i++)
		{
			if (param[i] != 0)
			{
				validCount++;
			}
		}
		return validCount;
	}


	public int doMaCalculate(double[] closes, Core taLib, int period, double[] ma)
	{
		MInteger outBegIdx = new MInteger();
		MInteger outNbElement = new MInteger();
		if(period != 1)
		{
			RetCode retCode = taLib.sma(0, closes.length - 1, closes, period, outBegIdx, outNbElement, ma);
		}
		else
		{
			outBegIdx.value = 0;
			for (int i = 0; i < ma.length; i++)
			{
				ma[i] = closes[i];
			}
		}
		return outBegIdx.value;
	}

	@Override
	public void onDrawLayer(Canvas pCanvas, Paint pPaint, Point pPoint)
	{
		drawKbar(pCanvas, pPaint, pPoint);
		drawMa(pCanvas, pPaint, pPoint);
	}

	private void drawMa(Canvas pCanvas, Paint pPaint, Point pPoint)
	{
//		获取坐标系
		Coordinate tCoordinate = m_callback.getCoordinate();
		//转化处理数据
		List<CandleData> tDataList = tMAList;
		float scaleFactor = tCoordinate.getScaleFactor();


		if (tDataList != null && tDataList.size() > 0)
		{
			int tListSize = tDataList.size();
//			int tStartIndex = tCoordinate.getStartIndex();

			float tXFirstPosition = tCoordinate.getXFirstPosition();
			float tXInterval = tCoordinate.getXInterval();
			float tYFirstposition = tCoordinate.getYFirstPosition();//  tCoordinate.getLayerRectF().bottom;
			// float tYFirstposition = tCoordinate.getYFirstPosition();
			float tYMaxValue = tCoordinate.getMaxValue();
			float tYMinValue = tCoordinate.getMinValue();
			float tYValueDist = tYMaxValue - tYMinValue;
			float tYValueLength = tCoordinate.getValueHeight();   //tCoordinate.getLayerRectF().bottom - tCoordinate.getLayerRectF().top;
			// float tYValueLength = tCoordinate.getYInterval() *
			// tCoordinate.getYIntervalCount();

			float tCurrentYPosition = 0.f;
			float tTempYPosition = 0.f;
			float tCurrentXPosition = 0.f;
			float tTempXPosition = 0.f;

			float tCurrentYPosition1 = 0.f;
			float tTempYPosition1 = 0.f;

			float tCurrentYPosition2 = 0.f;
			float tTempYPosition2 = 0.f;

			float tCurrentYPosition3 = 0.f;
			float tTempYPosition3 = 0.f;

			float tCurrentYPosition4 = 0.f;
			float tTempYPosition4 = 0.f;

			float tCurrentYPosition5 = 0.f;
			float tTempYPosition5 = 0.f;

			float tCurrentYPosition6 = 0.f;
			float tTempYPosition6 = 0.f;


			float tLineWidth = 2.f;
			pPaint.setAntiAlias(true);

			int m_bDigit = m_callback.getDigit();
			;//价格小数位数

			Symbol symbol = m_callback.getCoordinate().tSymBol;
			boolean tStartDraw1 = false, tStartDraw2 = false, tStartDraw3 = false, tStartDraw4 = false, tStartDraw5 = false, tStartDraw6 = false, tStartDraw7 = false;

			int tDotColor = BaseApp.getApp().getResources().getColor(R.color.sys_even);
			int tCulOffset = 1;

			int dataStart = getDataStartIndex(); //tStartIndex + getStartIndex();

			int moveStart = getMoveStartIndex();
			int displayCount = m_callback.getDisplayCount();//获取当前要显示的个数
			int minCount = Math.min(moveStart + displayCount, tListSize);//不能超出数据集宽度

			if (m_callback.isDrawAllData())//如果需要绘制全部数据的
			{
				dataStart = 0;
				moveStart = 0;
				minCount = tListSize;
			}

//			FDTLogUtils.d("tingchart",getLayerTypeEnum()+"layer from draw tStartIndex"+moveStart+" to mincount"+minCount);
			if (dataStart > tListSize)//如果数据量不足以算出一个值
			{
				return;
			}
			CandleData candle = null;
			for (int i = moveStart; i < minCount; i++)
			{
				//如果收盘价为空就跳过
				pPaint.setStrokeWidth(tLineWidth);

				candle = tDataList.get(i);

				float tMa1 = (float) candle.getDbOpen();
				float tMa2 = (float) candle.getDbClose();
				float tMa3 = (float) candle.getDbHigh();
				float tMa4 = (float) candle.getOpenBackward();
				float tMa5 = (float) candle.getCloseBackward();
				float tMa6 = (float) candle.getHighBackward();
				float tMa7 = (float) candle.getLowBackward();


				tTempXPosition = tCurrentXPosition;
				tCurrentXPosition = tXFirstPosition + tXInterval * (i - moveStart);
				if (tMa1 != 0.0)
				{

					tTempYPosition = tCurrentYPosition;
					tCurrentYPosition = tYFirstposition - ((tMa1 - tYMinValue) * tYValueLength / tYValueDist);

					if (tStartDraw1)
					{
						if (scaleFactor > 0.25)
						{
							RectF tRectF = tCoordinate.getLayerRectF();
							if (i > beginIdx1)//如果ma5，beginidx就是4，所以要到第6个点画线
							{
								pPaint.setColor(m_color1);
								drawLine(pCanvas, pPaint, tCurrentYPosition, tTempYPosition, tCurrentXPosition, tTempXPosition, tRectF);
							}
						}
					}
					else
					{
						tStartDraw1 = true;
					}
				}

				if (tMa2 != 0.0)
				{
					tTempYPosition1 = tCurrentYPosition1;
					tCurrentYPosition1 = tYFirstposition - ((tMa2 - tYMinValue) * tYValueLength / tYValueDist);

					if (tStartDraw2)
					{
						if (scaleFactor > 0.25)
						{
							RectF tRectF = tCoordinate.getLayerRectF();
							if (i > beginIdx2)//如果ma5，beginidx就是4，所以要到第6个点画线
							{
								pPaint.setColor(m_color2);
								drawLine(pCanvas, pPaint, tCurrentYPosition1, tTempYPosition1, tCurrentXPosition, tTempXPosition, tRectF);
							}
						}
					}
					else
					{
						tStartDraw2 = true;
					}
				}

				if (tMa3 != 0.0)
				{
					tTempYPosition2 = tCurrentYPosition2;
					tCurrentYPosition2 = tYFirstposition - ((tMa3 - tYMinValue) * tYValueLength / tYValueDist);

					if (tStartDraw3)
					{
						if (scaleFactor > 0.25)
						{
							RectF tRectF = tCoordinate.getLayerRectF();
							if (i > beginIdx3)//如果ma5，beginidx就是4，所以要到第6个点画线
							{
								pPaint.setColor(m_color3);
								drawLine(pCanvas, pPaint, tCurrentYPosition2, tTempYPosition2, tCurrentXPosition, tTempXPosition, tRectF);
							}
						}
					}
					else
					{
						tStartDraw3 = true;
					}
				}

				if (tMa4 != 0.0)
				{
					tTempYPosition3 = tCurrentYPosition3;
					tCurrentYPosition3 = tYFirstposition - ((tMa4 - tYMinValue) * tYValueLength / tYValueDist);

					if (tStartDraw4)
					{
						if (scaleFactor > 0.25)
						{
							RectF tRectF = tCoordinate.getLayerRectF();
							if (i > beginIdx4)//如果ma5，beginidx就是4，所以要到第6个点画线
							{
								pPaint.setColor(m_color4);
								drawLine(pCanvas, pPaint, tCurrentYPosition3, tTempYPosition3, tCurrentXPosition, tTempXPosition, tRectF);
							}
						}
					}
					else
					{
						tStartDraw4 = true;
					}
				}

				if (tMa5 != 0.0)
				{
					tTempYPosition4 = tCurrentYPosition4;
					tCurrentYPosition4 = tYFirstposition - ((tMa5 - tYMinValue) * tYValueLength / tYValueDist);

					if (tStartDraw5)
					{
						if (scaleFactor > 0.25)
						{
							RectF tRectF = tCoordinate.getLayerRectF();
							if (i > beginIdx5)//如果ma5，beginidx就是4，所以要到第6个点画线
							{
								pPaint.setColor(m_color5);
								drawLine(pCanvas, pPaint, tCurrentYPosition4, tTempYPosition4, tCurrentXPosition, tTempXPosition, tRectF);
							}
						}
					}
					else
					{
						tStartDraw5 = true;
					}
				}

				if (tMa6 != 0.0)
				{
					tTempYPosition5 = tCurrentYPosition5;
					tCurrentYPosition5 = tYFirstposition - ((tMa6 - tYMinValue) * tYValueLength / tYValueDist);

					if (tStartDraw6)
					{
						if (scaleFactor > 0.25)
						{
							RectF tRectF = tCoordinate.getLayerRectF();
							if (i > beginIdx6)//如果ma5，beginidx就是4，所以要到第6个点画线
							{
								pPaint.setColor(m_color6);
								drawLine(pCanvas, pPaint, tCurrentYPosition5, tTempYPosition5, tCurrentXPosition, tTempXPosition, tRectF);
							}
						}
					}
					else
					{
						tStartDraw6 = true;
					}
				}

				if (tMa7 != 0.0)
				{
					tTempYPosition6 = tCurrentYPosition6;
					tCurrentYPosition6 = tYFirstposition - ((tMa7 - tYMinValue) * tYValueLength / tYValueDist);

					if (tStartDraw7)
					{
						if (scaleFactor > 0.25)
						{
							RectF tRectF = tCoordinate.getLayerRectF();
							if (i > beginIdx7)//如果ma5，beginidx就是4，所以要到第6个点画线
							{
								pPaint.setColor(m_color7);
								drawLine(pCanvas, pPaint, tCurrentYPosition6, tTempYPosition6, tCurrentXPosition, tTempXPosition, tRectF);
							}
						}
					}
					else
					{
						tStartDraw7 = true;
					}
				}


				if (((tCurrentXPosition - tXInterval / 2) <= pPoint.x && pPoint.x < (tCurrentXPosition + tXInterval / 2)) || ((i == minCount - 1) && pPoint.x > tCurrentXPosition + tXInterval / 2) || (i == moveStart && pPoint.x < tCurrentXPosition - tXInterval / 2))
				{
					float tValueaa = (tYFirstposition - pPoint.y) * tYValueDist / tYValueLength + tYMinValue;
					m_ma1 = FormatUtil.getFormatString(tMa1, m_bDigit);
					m_ma2 = FormatUtil.getFormatString(tMa2, m_bDigit);
					m_ma3 = FormatUtil.getFormatString(tMa3, m_bDigit);
					m_ma4 = FormatUtil.getFormatString(tMa4, m_bDigit);
					m_ma5 = FormatUtil.getFormatString(tMa5, m_bDigit);
					m_ma6 = FormatUtil.getFormatString(tMa6, m_bDigit);
					m_ma7 = FormatUtil.getFormatString(tMa7, m_bDigit);
				}


				if (tDataList.get(i).m_bDrawDot)
				{
					pPaint.setColor(tDotColor);
					pCanvas.drawCircle(tCurrentXPosition, tCurrentYPosition, tLineWidth * 2, pPaint);
				}
			}

			candle = tDataList.get(minCount - 1);
			if (candle != null)
			{
				m_lastMa1 = FormatUtil.getFormatString(candle.getDbOpen(), m_bDigit);
				m_lastMa2 = FormatUtil.getFormatString(candle.getDbClose(), m_bDigit);
				m_lastMa3 = FormatUtil.getFormatString(candle.getDbHigh(), m_bDigit);
				m_lastMa4 = FormatUtil.getFormatString(candle.getOpenBackward(), m_bDigit);
				m_lastMa5 = FormatUtil.getFormatString(candle.getCloseBackward(), m_bDigit);
				m_lastMa6 = FormatUtil.getFormatString(candle.getHighBackward(), m_bDigit);
				m_lastMa7 = FormatUtil.getFormatString(candle.getLowBackward(), m_bDigit);
			}

		}
		else
		{
			resetIndicatorData();
		}
	}

	private void resetIndicatorData()
	{
		m_ma1 = "";
		m_ma2 = "";
		m_ma3 = "";
		m_ma4 = "";
		m_ma5 = "";
		m_ma6 = "";
		m_ma7 = "";
		m_lastMa1 = "";
		m_lastMa2 = "";
		m_lastMa3 = "";
		m_lastMa4 = "";
		m_lastMa5 = "";
		m_lastMa6 = "";
		m_lastMa7 = "";
	}

	private void drawLine(Canvas pCanvas, Paint pPaint, float tCurrentYPosition, float tTempYPosition, float tCurrentXPosition, float tTempXPosition, RectF tRectF)
	{
		pCanvas.drawLine(tTempXPosition, tTempYPosition, tCurrentXPosition, tCurrentYPosition, pPaint);
	}


	/**
	 * draw layer left－top text
	 * 获取指标值进行显示
	 *
	 * @param pCanvas
	 * @param pPaint
	 */
	@Override
	public void drawFloatIndicatorText(boolean longtouch, Canvas pCanvas, Paint pPaint)
	{

		if (!m_callback.needFloatIndicatorText())
		{
			return;
		}

		txtSize = DimentionsUtil.dp2px(10);

		boolean m_ma_on = true;//FDTStorage.getInstance().getInteger(
				//SaveKey.CHART_INDICATOR_MA + "on", SaveKey.CHART_INDICATOR_PARAM, 1) == 1;

		if (!m_ma_on)
		{
			return;
		}

		List<CandleData> tDataList = m_callback.getCandleDataList();
		if (tDataList == null || tDataList.size() == 0)
		{
			resetIndicatorData();
		}

		Coordinate tCoordinate = m_callback.getCoordinate();
		RectF tLayerRect = tCoordinate.getLayerRectF();
		RectF tChartRect = tCoordinate.getChartRectF();

		float tLeftPadding = DimentionsUtil.dp2px(0);
		float tButtomPadding = DimentionsUtil.dp2px(2);
		float tWordPadding = txtSize;

		//根据需要添加相应的均线数据上去
		pPaint.reset();
		pPaint.setTextSize(txtSize);
		pPaint.setAntiAlias(true);
		float textHeight = txtSize;
		mTxtXLocation = tLayerRect.left + tLeftPadding;
		mNeedDrawIndex = 0;
		float space = 0.f;

		mIsPortrait = true;//ForexApplication.isPortrait();
		if (validPeriodCount > 4 && mIsPortrait)
		{
			space = (tLayerRect.top - tChartRect.top - 2 * textHeight) / 3.f;
		}
		else
		{
			space = (tLayerRect.top - tChartRect.top - textHeight) / 2.f;
		}
		space = space < 0 ? 0.f : space;


		drawIndicator(mPeriod1, m_ma1, m_lastMa1, m_color1, longtouch, pCanvas, pPaint, tLayerRect, tChartRect, tLeftPadding, tButtomPadding, tWordPadding, textHeight, space);
		drawIndicator(mPeriod2, m_ma2, m_lastMa2, m_color2, longtouch, pCanvas, pPaint, tLayerRect, tChartRect, tLeftPadding, tButtomPadding, tWordPadding, textHeight, space);
		drawIndicator(mPeriod3, m_ma3, m_lastMa3, m_color3, longtouch, pCanvas, pPaint, tLayerRect, tChartRect, tLeftPadding, tButtomPadding, tWordPadding, textHeight, space);
		drawIndicator(mPeriod4, m_ma4, m_lastMa4, m_color4, longtouch, pCanvas, pPaint, tLayerRect, tChartRect, tLeftPadding, tButtomPadding, tWordPadding, textHeight, space);
		drawIndicator(mPeriod5, m_ma5, m_lastMa5, m_color5, longtouch, pCanvas, pPaint, tLayerRect, tChartRect, tLeftPadding, tButtomPadding, tWordPadding, textHeight, space);
		drawIndicator(mPeriod6, m_ma6, m_lastMa6, m_color6, longtouch, pCanvas, pPaint, tLayerRect, tChartRect, tLeftPadding, tButtomPadding, tWordPadding, textHeight, space);
		drawIndicator(mPeriod7, m_ma7, m_lastMa7, m_color7, longtouch, pCanvas, pPaint, tLayerRect, tChartRect, tLeftPadding, tButtomPadding, tWordPadding, textHeight, space);


	}

	public void drawIndicator(int period, String cValue, String lastValue, int color, boolean longtouch, Canvas pCanvas, Paint pPaint, RectF tLayerRect, RectF tChartRect, float tLeftPadding, float tButtomPadding, float tWordPadding, float textHeight, float tSpace)
	{
		if (period != 0)
		{
			String indicatorText;
			if (longtouch)
			{
				indicatorText = "MA" + period + " " + cValue;
			}
			else
			{
				indicatorText = "MA" + period + " " + lastValue;
			}
			float textWidth = pPaint.measureText(indicatorText);
			pPaint.setColor(color);
//
			if(mIsPortrait)
			{
				float textY = tLayerRect.top + tSpace + txtSize;
				pCanvas.drawText(indicatorText, mTxtXLocation, textY, pPaint);
			}
			else
			{
				float textY = tLayerRect.top + tSpace + txtSize;
				pCanvas.drawText(indicatorText, mTxtXLocation, textY, pPaint);
			}
			mTxtXLocation += textWidth + tWordPadding;
			mNeedDrawIndex++;
		}
	}

}
